package timeClock;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;	
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.*;
import java.net.URL;
import java.io.FileWriter;
import java.io.*;
import java.net.*;



public class menu implements ActionListener
	{		
		JButton jbnClear;
		
		JButton jbnClockIN;
		
		JButton jbnClockOUT;
		
		JButton jbnSave;
		
		JTextField jtfUser;
		
		JTextField jpwdPass;
		
		JLabel jlbUser;
		
		JLabel jlbPass;
		
		JTextPane jPane;
		
		JScrollPane jScroll;
		
		readFile read;
		
		URL url;
		
		getinfo info = new getinfo();
		clock clock = new clock();
		
		
		
		JFrame jfMenu;
		public static void main(String args[]){
			new menu(); 
		}
		
		public menu()
		{ 				
			createGUI();
			
		}
		
		public void createGUI(){
			
			
			jfMenu = new JFrame("Menu");
			//Arrange components on contentPane and set Action Listeners to each JButton
			arrangeComponents();
			
			jfMenu.setSize(320, 190);
			jfMenu.setLayout(null);
			jfMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jfMenu.add(jbnClockOUT);
			jfMenu.add(jbnClockIN);
			jfMenu.add(jbnSave);
			jfMenu.add(jlbPass);
			jfMenu.add(jlbUser);
			jfMenu.add(jpwdPass);
			jfMenu.add(jtfUser);
			jfMenu.add(jbnClear);
			jfMenu.add(jScroll);
			jfMenu.setVisible(true);
			
			
		}
		
		public void arrangeComponents(){
			
			read = new readFile();
			
			jPane = new JTextPane();
			jPane.setEditable(false);
			jScroll = new JScrollPane();
			jScroll.setBounds(20,100,280,55);
			jScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
			jScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jScroll.getViewport().add(jPane);
			jScroll.getHorizontalScrollBar().setValue(9001);
			jScroll.getVerticalScrollBar().setValue(9001);
			try {
				ClassLoader cldr = this.getClass().getClassLoader();
				url = cldr.getResource("blank.html");
				jPane.setPage(url);
			 
			
			
			url = cldr.getResource("user.html");
			jlbUser = new JLabel("User");
			jlbUser.setBounds(20,20,60,20);	
			jtfUser = new JTextField(read.read(url));
			jtfUser.setBounds(80,20,220,20);		
			
			url = cldr.getResource("pass.html");
			jlbPass = new JLabel("Password");
			jlbPass.setBounds(20,40,60,20);
			jpwdPass = new JTextField(read.read(url));
			jpwdPass.setBounds(80,40,220,20);
			
			jbnClockOUT = new JButton("Clock out");
			jbnClockOUT.setBounds(93,70,73,20);
			
			jbnClockIN = new JButton("Clock in");
			jbnClockIN.setBounds(166,70,73,20);
			
			jbnClear = new JButton ("Exit");
			jbnClear.setBounds(239,70,60,20);
			
			jbnSave = new JButton("Save Cred.");
			jbnSave.setBounds(20,70,73,20);
			
			}
			
			catch (Exception e) {
				e.printStackTrace();
			}
			
			
			jbnSave.addActionListener(this);			
			jbnClear.addActionListener(this);			
			jbnClockOUT.addActionListener(this);
			jbnClockIN.addActionListener(this);			
		}
		
		public void actionPerformed (ActionEvent e) 
		{
			
			if (e.getSource () == jbnClockOUT){
				clock.out(jtfUser.getText(),jpwdPass.getText());
				System.out.println(jtfUser.getText() + " " + jpwdPass.getText() + " Clock out!");
				jPane.setText(info.get());
								
				
			}
			
			if (e.getSource () == jbnClockIN){
				clock.in(jtfUser.getText(),jpwdPass.getText());
				System.out.println(jtfUser.getText() + " " + jpwdPass.getText() + " Clock in!");
				jPane.setText(info.get());
			}
			
			if (e.getSource() == jbnSave){
				try {
					BufferedWriter out = new BufferedWriter(new FileWriter("user.html"));
					out.write(jtfUser.getText());
					out.close();
					
					BufferedWriter out2 = new BufferedWriter(new FileWriter("pass.html"));
					out2.write(jpwdPass.getText());
					out2.close();
					
				} catch (IOException q) {
					q.printStackTrace();
				}
				
				
			}
			
			if (e.getSource () == jbnClear){
				System.exit(0);
			}
			
			
			
			
			
		}
		// Save the Person into the Address Book 
		
		
	}